import 'dart:io';
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:scanner/config/wj_color.dart';
import 'package:scanner/widgets/progress_widget.dart';



class MkImage extends StatelessWidget {
  final String? path;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final ImageWidgetBuilder? imageBuilder;
  final PlaceholderWidgetBuilder? placeholder;
  final LoadingErrorWidgetBuilder? errorWidget;
  final EdgeInsetsGeometry? padding;
  final BorderRadiusGeometry? borderRadius;

  ///修剪圆角的大小
  final BorderRadiusGeometry? clipRadius;
  final Color? color;
  final AlignmentGeometry? alignment;
  final Alignment? imgAlignment;
  final ColorFilter? colorFilter;
  final List<BoxShadow>? boxShadow;
  final Widget? child;
  final double? borderWidth;
  final Color? borderColor;

  final bool? blur;

  final int? maxWidthDiskCache;
  final int? maxHeightDiskCache;

  const MkImage({
    super.key,
    required this.path,
    this.width,
    this.height,
    this.fit,
    this.imageBuilder,
    this.placeholder,
    this.errorWidget,
    this.padding,
    this.borderRadius,
    this.color,
    this.alignment,
    this.imgAlignment,
    this.colorFilter,
    this.boxShadow,
    this.child,
    this.clipRadius,
    this.borderWidth,
    this.borderColor,
    this.blur,
    this.maxWidthDiskCache,
    this.maxHeightDiskCache,
  });

  @override
  Widget build(BuildContext context) {
    bool isBlur = blur ?? false;

    Widget child = ImageFiltered(
      imageFilter: ImageFilter.blur(sigmaX: isBlur ? 3 : 0, sigmaY: isBlur ? 3 : 0),
      child: childWidget(),
    );

    if (clipRadius == null) {
      return child;
    } else {
      if (borderWidth != null) {
        return ClipRRect(
          borderRadius: clipRadius ?? BorderRadius.zero,
          child: Container(
              width: (width! + 2 * borderWidth!).w,
              height: (width! + 2 * borderWidth!).w,
              color: borderColor ?? WJColor.color58,
              alignment: Alignment.center,
              child: ClipRRect(borderRadius: clipRadius ?? BorderRadius.zero, child: child)),
        );
      }
      return ClipRRect(
        borderRadius: clipRadius ?? BorderRadius.zero,
        child: child,
      );
    }
  }

  Widget childWidget() {
    if ((path ?? '').isEmpty) return Container();

    if (path!.startsWith('http')) {
      return _netImg(
        imageUrl: path!,
        width: width?.w,
        height: height?.w,
        fit: fit,
        imageBuilder: imageBuilder,
        placeholder: placeholder,
        errorWidget: errorWidget,
      );
    }
    if (path!.startsWith('assets')) {
      return Image.asset(
        path!,
        width: width?.w,
        height: height?.w,
        fit: fit,
        color: color,
      );
    }

    return Image.file(
      File(path!),
      width: width?.w,
      height: height?.w,
      fit: fit,
      color: color,
    );
  }

  Widget _netImg({
    required String imageUrl,
    double? width,
    double? height,
    BoxFit? fit,
    Color? color,
    ImageWidgetBuilder? imageBuilder,
    PlaceholderWidgetBuilder? placeholder,
    LoadingErrorWidgetBuilder? errorWidget,
  }) {
    if (imageUrl.isEmpty) {
      return SizedBox(width: width, height: height);
    }
    return CachedNetworkImage(
      imageUrl: imageUrl,
      width: width,
      height: height,
      fit: fit,
      color: color,
      alignment: imgAlignment ?? Alignment.center,
      imageBuilder: imageBuilder,
      useOldImageOnUrlChange: true,
      maxWidthDiskCache: maxWidthDiskCache ?? 500,
      maxHeightDiskCache: maxHeightDiskCache ?? 500,
      placeholder: placeholder ?? (a, b) => const WJProgressWidget(),
      // errorWidget: errorWidget ??
      //         (a, b, c) {
      //       print('2024-07-12 10:13:31--->img error:$imageUrl');
      //       return Container(
      //           width: width,
      //           height: height,
      //           color: CoColors.color999,
      //           child: Column(
      //             mainAxisAlignment: MainAxisAlignment.center,
      //             children: [
      //               Image.asset(
      //                 Assets.assetsIconPicLoadFail,
      //                 width: 38.w,
      //                 height: 38.w,
      //                 fit: BoxFit.cover,
      //               ),
      //               CommonWidgets.getText('Failed to load',
      //                   fontSize: 10, fontWeight: FontWeight.bold, textColor: Colors.white.withOpacity(0.65))
      //             ],
      //           ));
      //     },
    );
  }
}
